/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Course;
import me.zhengjie.modules.system.domain.Courseeditteacheraudit;
import me.zhengjie.modules.system.domain.Teacher;
import me.zhengjie.modules.system.service.CourseService;
import me.zhengjie.modules.system.service.CourseeditteacherauditService;
import me.zhengjie.modules.system.domain.vo.CourseeditteacherauditQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.modules.system.service.TeacherService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2025-06-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程编辑教师管理")
@RequestMapping("/api/courseeditteacheraudit")
public class CourseeditteacherauditController {

    private final CourseeditteacherauditService courseeditteacherauditService;

    private final TeacherService teacherService;

    private final CourseService courseService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('courseeditteacheraudit:list')")
    public void exportCourseeditteacheraudit(HttpServletResponse response, CourseeditteacherauditQueryCriteria criteria) throws IOException {
        courseeditteacherauditService.download(courseeditteacherauditService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询课程编辑教师")
    @ApiOperation("查询课程编辑教师")
//    @PreAuthorize("@el.check('courseeditteacheraudit:list')")
    public ResponseEntity<List<Courseeditteacheraudit>> queryCourseeditteacheraudit(CourseeditteacherauditQueryCriteria criteria, Page<Object> page){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("courseId",criteria.getCourseId());
        List<Courseeditteacheraudit>  courseeditteacherauditList = courseeditteacherauditService.list(queryWrapper);

        System.out.println("===courseeditteacherauditList==="+courseeditteacherauditList);
        List<Integer> teacherIds = new ArrayList<>();
        for(Integer i=0;i<courseeditteacherauditList.size();i++){
            teacherIds.add(courseeditteacherauditList.get(i).getUserId());
        }

        List<Teacher> teacherList = new ArrayList<>();
        if(teacherIds.size()!=0){
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("userId",teacherIds);
            teacherList = teacherService.list(queryWrapper1);
        }

        for(Integer i=0;i<courseeditteacherauditList.size();i++){

            for(Integer j =0;j<teacherList.size();j++){
                if(courseeditteacherauditList.get(i).getUserId() == Math.toIntExact(teacherList.get(j).getUserid())){
                    courseeditteacherauditList.get(i).setTeacherName(teacherList.get(j).getUsername());
                }
            }
        }
        return new ResponseEntity<>(courseeditteacherauditList,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增课程编辑教师")
    @ApiOperation("新增课程编辑教师")
//    @PreAuthorize("@el.check('courseeditteacheraudit:add')")
    public ResponseEntity<Object> createCourseeditteacheraudit(@Validated @RequestBody Courseeditteacheraudit resources){
        Course course = courseService.getById(resources.getCourseId());
        course.setState2(Integer.valueOf(resources.getState()));
        courseService.update(course);
        courseeditteacherauditService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改课程编辑教师")
    @ApiOperation("修改课程编辑教师")
//    @PreAuthorize("@el.check('courseeditteacheraudit:edit')")
    public ResponseEntity<Object> updateCourseeditteacheraudit(@Validated @RequestBody Courseeditteacheraudit resources){
        courseeditteacherauditService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除课程编辑教师")
    @ApiOperation("删除课程编辑教师")
//    @PreAuthorize("@el.check('courseeditteacheraudit:del')")
    public ResponseEntity<Object> deleteCourseeditteacheraudit(@RequestBody List<Integer> ids) {
        courseeditteacherauditService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}